#include "MultiplayerGame.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
MultiplayerGame::MultiplayerGame(void)
{

}

MultiplayerGame::~MultiplayerGame(void)
{
}

int main(int argc, char *argv[])
{/*
		    _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF );
			_crtBreakAlloc = 224;*/
	try
	{
		bool quit = false;
        while (!quit)
        {
			StartWindow startWindow;
            startWindow.excecute();
            switch (startWindow.getOptionSelected())
			{
				case(1):
					{
						GameServer server (startWindow.getSceneSelected(), startWindow.getPort());
						GameClient client (startWindow.getUserName(), startWindow.getGameMode(), startWindow.getPort());
						PlaygroundWindow playgroundWindow(&client);

						Thread* serverThread = ThreadPool::getInstance()->execute(&server);
						while(!server.isReady())
							Sleep(0);
						
						Thread* clientThread =  ThreadPool::getInstance()->execute(&client);
	
						while(!client.isReady())
							Sleep(0);

						playgroundWindow.excecute();
						
						client.shutdown();
						clientThread->join(INFINITE);
						server.shutdown();
						serverThread->join(INFINITE);
						delete clientThread;
						delete serverThread;
						_CrtDumpMemoryLeaks();
					}
					break;
				case(2):
					{	
						GameClient client (startWindow.getUserName(),startWindow.getGameMode(), startWindow.getIP(), startWindow.getPort());
						PlaygroundWindow playgroundWindow(&client);
						
						Thread* clientThread =  ThreadPool::getInstance()->execute(&client);
	
						while(!client.isReady())
							Sleep(0);

						playgroundWindow.excecute();
						
						client.shutdown();
						clientThread->join(INFINITE);
					}
					break;
				default:
					quit=true;
			}
        }
    }
	catch (SDL_Exception &ex)
    {
        cout << ex.getMessage() << endl;
    }
	
	system("PAUSE");
	return 0;
}
